package component.listener;



import javax.annotation.Resource;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import component.Action;
import dto.AdSpaceToMDto;
import util.JsonUtil;

public class AdvertiseStatusListener implements MessageListener {
	
	private final static Logger logger = LogManager.getLogger(AdvertiseStatusListener.class);
	
	@Resource(name="AdvertiseStatusAction")
	private Action advertiseAction;

    public void onMessage(Message message) {
        if (message instanceof TextMessage) {
            try {
                TextMessage textMessage = (TextMessage) message;
                String reString = textMessage.getText();
                System.out.println("adv admin:"+reString);
                AdSpaceToMDto adSpaceToMDto = (AdSpaceToMDto) JsonUtil.toJsonObject(reString, AdSpaceToMDto.class);
                advertiseAction.action(adSpaceToMDto);
                message.acknowledge();
            }
            catch (JMSException ex) {
            	logger.error(ex);
            }
        }
        else {
        	logger.info("Message must be of type TextMessage");
        }
    }
}
